//
//  DownloadHistoryViewController.m
//  IntegralDog
//
//  Created by HanenDev on 16/1/25.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "DownloadHistoryViewController.h"
#import "DownloadHistoryCell.h"
#import "Macro.h"
#import "PersonalNetTool.h"
#import "HistoryModel.h"
#import "HistoryCell.h"
#define SCREENWIDTH self.view.bounds.size.width
#define SCREENHEIGHT self.view.bounds.size.height

@interface DownloadHistoryViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_rowsArr;
    UITableView    *_tableView;
    NSArray        *_dateArray;
    NSMutableArray *_SecArr;
    NSMutableArray *_Arr;
    
    int            _section;
}

@property (nonatomic,strong)NSArray *picArr;
@property (nonatomic,strong)NSArray *titleArr;
@property (nonatomic,strong)NSArray *editionArr;
@property (nonatomic,strong)NSArray *countArr;
@end

@implementation DownloadHistoryViewController
//- (void)viewDidAppear:(BOOL)animated
//{
//    NSString *dateStr = _dateArray[0];
//    NSLog(@"dateStr   %@=-=-=-=-==",dateStr);
//    [PersonalNetTool getdownloadRecordWithpage:1 rows:20 date:@"" complationBlock:^(NSDictionary *dic) {
//        NSLog(@"tttttttttttttttttttt     %@",dic);
//        [_rowsArr removeAllObjects];
//        for (NSArray *arr in [dic objectForKey:@"rows"]) {
//            
//            [_rowsArr addObject:arr];
//        }
//        [_tableView reloadData];
//        
//    }];////////////////
//    
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"下载记录";
    _rowsArr = [[NSMutableArray alloc] init];
    _SecArr = [[NSMutableArray alloc] init];
    _Arr = [[NSMutableArray alloc] init];
    _section = 0;
    UIButton *backBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 12, 22)];
    [backBtn setImage:[UIImage imageNamed:@"Btn_Nourmal_Jiantou"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"Btn_Push_Jiantou"] forState:UIControlStateSelected];
    [backBtn addTarget:self action:@selector(goToBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:backBtn];
    _picArr = @[@"Btn_Normal_Xiangji_03",@"Btn_Normal_Xiecheng_07",@"Btn_Normal_weixin_10",@"Btn_Normal_Bantang_12",@"Btn_Normal_Kugou_14",@"Btn_Normal_In_01"];
    _titleArr = @[@"相机360-动感贴纸，炫酷小视频",@"携程旅行-定酒店机票火车票",@"微信",@"半塘-帮你买到好东西",@"酷狗音乐-《我是歌手》第四季",@"in-超萌贴纸美图相机"];
    _editionArr = @[@"版本9.0.0  134MB",@"版本6.12.4  98.4MB",@"版本87.6.0  134MB",@"版本9.0.0  884MB",@"版本88.6.8  134MB",@"版本8.11.3  112MB"];
    _countArr = @[@"500",@"450",@"360",@"345",@"226",@"186"];
    [self createTableView];
    
    [self initData];
    
    _rowsArr =[[NSMutableArray alloc]initWithCapacity:0];
    
}
- (void)initData{
    
    NSDictionary *dic =@{@"token":TOKEN,@"uid":@(UID)};
    [DataTool postWithUrl:DOWNLOAD_DATE_LIST_URL parameters:dic success:^(id data) {
        [_SecArr removeAllObjects];
        NSDictionary *jsonDic = IntegrationJsonParserWithData(data);
        NSLog(@"%@+++++datelist",jsonDic);
        //NSMutableArray *array = jsonDic[@"list"];
        _dateArray = jsonDic[@"list"];
        NSLog(@"%@++++++++_dateArray",_dateArray);
        [_tableView reloadData];
        for (int i = 0; i < _dateArray.count; i++) {
            [self createDataWith:i];
        }
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)createDataWith:(int)num{
    NSNumber *pageNum = [NSNumber numberWithInt:1];
    NSNumber *rowsNum = [NSNumber numberWithInt:20];
    NSLog(@".///////////////////////////        %d",num);
    NSDictionary *dic =@{@"token":TOKEN,@"uid":@(UID),@"page":pageNum,@"rows":rowsNum,@"date":_dateArray[num]};
    
    [DataTool postWithUrl:DOWNLOAD_RECORD_URL parameters:dic success:^(id data) {
        NSDictionary *jsonDic = IntegrationJsonParserWithData(data);
        NSLog(@"%@+++++0.0.0.0.0.0.0.",jsonDic);
        NSMutableArray *array = jsonDic[@"rows"];
        [_Arr addObject:array];
        //[_rowsArr removeAllObjects];
        HistoryModel *model1 = [[HistoryModel alloc] init];
        [_rowsArr addObject:model1];
        
        for (int i=0; i<array.count; i++) {
            HistoryModel *model = [[HistoryModel alloc]init];
            [model setValuesForKeysWithDictionary:array[i]];
            
            [_rowsArr addObject:model];
        }
        [_tableView reloadData];
        
        [_SecArr addObject:[NSNumber numberWithInt:_section]];
        _section = (int)_rowsArr.count;
        
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];

}
- (void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGHT - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 100.0f;
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
}

#pragma mark --------------------UITableview Delegate----------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _rowsArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (NSNumber *a in _SecArr) {
        if ([a intValue] == indexPath.row) {
            return 40;
        }
    }
    return 80;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 40.0f;
//}
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *view=[[UIView alloc]init];
//    view.backgroundColor = bGGrayColor;
//    UIImageView *picView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 20, 20)];
//    picView.image = [UIImage imageNamed:@"Btn_Normal_Shijianjilu"];
//    [view addSubview:picView];
//    
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30+10, 10, SCREENWIDTH, 20)];
//    label.textColor = fontLightGrayColor2;
//    label.font = [UIFont systemFontOfSize:14.0f];
//    if (section == 0) {
//         label.text = @"下载于 2016.01.31";
//    }else if (section ==1){
//        label.text = @"下载于 2016.02.18";
//    }
//    label.text = [NSString stringWithFormat:@"下载于 %@",_dateArray[section]];
//    
//    label.textColor = [UIColor grayColor];
//    [view addSubview:label];
//    return view;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    static NSString *str = @"str";
    DownloadHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    HistoryCell *hiscell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (cell == nil) {
        cell = [[DownloadHistoryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (hiscell == nil) {
        hiscell = [[HistoryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    hiscell.selectionStyle = UITableViewCellSelectionStyleNone;
    //NSLog(@"...........%@",_Arr);
    if (_rowsArr.count != 0) {
                cell.model = _rowsArr[indexPath.row];
//        NSLog(@"         %@",_SecArr);
//        for (NSNumber *a in _SecArr) {
//            if ([a intValue] == indexPath.row) {
//                hiscell.label.text = [NSString stringWithFormat:@"下载于%@",_dateArray[0]];
//                return hiscell;
//            }
//        }
    
    for (int i = 0 ; i < _SecArr.count; i++) {
        if (indexPath.row == [[_SecArr objectAtIndex:i] integerValue]) {
            hiscell.label.text = [NSString stringWithFormat:@"下载于%@",_dateArray[i]];
            return hiscell;
        }
      }
    }
    //NSLog(@",,,,,,,  %d",_rowsArr.count);
       //_section = indexPath.section;
//    HistoryModel *model = _rowsArr[indexPath.row];
//    
//    NSString *imgStr = [NSString stringWithFormat:@"%@%@",IMAGEURL,model.logo];
//    [cell.imagView sd_setImageWithURL:[NSURL URLWithString:imgStr]];
//    
//    cell.nameLabel.text = model.title;
//    
//    cell.editionLabel.text = model.version;
//    
//    cell.sizeLabel.text = model.size;
//    
//    cell.updataLabel.text = @"新功能已更新";
//    
//    cell.picView.image = [UIImage imageNamed:@"Btn_Normal_Jifenganniu"];
//    
//    cell.countLabel.text = model.reward;
    
    
//    if (indexPath.section == 0) {
//        cell.imagView.image = [UIImage imageNamed:_picArr[indexPath.row]];
//        
//        cell.nameLabel.text = _titleArr[indexPath.row];
//        
//        cell.editionLabel.text = _editionArr[indexPath.row];
//        
//        cell.updataLabel.text = @"新功能已更新";
//        
//        cell.picView.image = [UIImage imageNamed:@"Btn_Normal_Jifenganniu"];
//        
//        cell.countLabel.text = _countArr[indexPath.row];
//    }else if (indexPath.section == 1){
//        cell.imagView.image = [UIImage imageNamed:_picArr[indexPath.row + 4]];
//        
//        cell.nameLabel.text = _titleArr[indexPath.row +4];
//        
//        cell.editionLabel.text = _editionArr[indexPath.row + 4];
//        
//        cell.updataLabel.text = @"新功能已更新";
//        
//        cell.picView.image = [UIImage imageNamed:@"Btn_Normal_Jifenganniu"];
//        
//        cell.countLabel.text = _countArr[indexPath.row + 4];
//    }
    
    
    return cell;
}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
